﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Windows8Accelerator.Core.Xaml.Helpers;

namespace Windows8Accelerator.Core.Xaml.Controls
{
    public class DataTemplateSelectorControl : ContentControl
    {
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            var groupItemControl = ControlHelper.GetParentOf<GroupItemControl>(this);
            if (groupItemControl != null)
            {
                SetBinding(DataTemplateSelectorControl.TemplateSelectorProperty,
                    new Binding("ItemTemplateSelector") { Source = groupItemControl });
                SetBinding(DataTemplateSelectorControl.DefaultTemplateProperty,
                    new Binding("ItemTemplate1") { Source = groupItemControl });
                ContentTemplate = SelectTemplate(Content, this);
            }
        }

        protected override void OnContentChanged(object oldContent, object newContent)
        {
            base.OnContentChanged(oldContent, newContent);
            ContentTemplate = SelectTemplate(newContent, this);
        }

        public virtual DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            if (TemplateSelector != null)
                return TemplateSelector.SelectTemplate(item, container);
            return DefaultTemplate;
        }

        public DataTemplateSelector TemplateSelector
        {
            get { return (DataTemplateSelector)GetValue(TemplateSelectorProperty); }
            set { SetValue(TemplateSelectorProperty, value); }
        }
        public static readonly DependencyProperty TemplateSelectorProperty =
            DependencyProperty.Register("TemplateSelector", typeof(DataTemplateSelector), typeof(DataTemplateSelectorControl), new PropertyMetadata(TemplateSelectorPropertyChanged));

        private static void TemplateSelectorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((DataTemplateSelectorControl)d).UpdateTemplate();
        }

        public DataTemplate DefaultTemplate
        {
            get { return (DataTemplate)GetValue(DefaultTemplateProperty); }
            set { SetValue(DefaultTemplateProperty, value); }
        }
        public static readonly DependencyProperty DefaultTemplateProperty =
            DependencyProperty.Register("DefaultTemplate", typeof(DataTemplate), typeof(DataTemplateSelectorControl), new PropertyMetadata(DefaultTemplatePropertyChanged));

        private static void DefaultTemplatePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((DataTemplateSelectorControl)d).UpdateTemplate();
        }

        private void UpdateTemplate()
        {
            ContentTemplate = SelectTemplate(Content, this);
        }
    }
}
